var dbConfig = require('../util/dbconfig')


//获取login登录
getLoginUser = (req, res) => {
	let name = req.body.name
	let password = req.body.password
	var sql = 'select * from user where name=?';
	var sqlArr = [name];
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			if (password == data[0].password) {
				res.send({
					'code': 0,
					'data': {
						'msg': '登录成功！',
						'token': 'Authorization:' + Math.random(),
						'expireAt': new Date(new Date().getTime() + 30 * 60 * 1000),
						'user': {
							'address': "石嘴山市",
							'avatar': "https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png",
							'name': "ICZER",
							'position':{
								'CN': "Java工程师 | 蚂蚁金服-计算服务事业群-微信平台部",
								'HK': "Java工程師 | 螞蟻金服-計算服務事業群-微信平台部",
								'US': "Java engineer | Ant financial - Computing services business group - WeChat platform division"
							}
						},
						'permissions': [{
							id: 'queryForm',
							operation: ['add', 'edit', 'delete']
						}],
						'roles': [{
							id: 'admin',
							operation: ['add', 'edit', 'delete']
						}]
					},
					'message': "下午好，欢迎回来",
				})
			} else {
				res.send({
					'code': 1,
					'data': {
						'msg': '密码错误！'
					}
				})
			}

		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
getLoginRoutes = (req, res) => {
	res.send({
		'code': 0,
		'data': [{
			router: 'root',
			children: [{
					router: 'dashboard',
					children: ['workplace', 'analysis'],
				},
				{
					router: 'form',
					children: ['basicForm', 'stepForm', 'advanceForm']
				},
				{
					router: 'basicForm',
					name: '验权表单',
					icon: 'file-excel',
					authority: 'form'
				}
			]
		}]
	})
}
// 获取chart表
// sql语句
getCate = (req, res) => {
	//查表所有数据
	var sql = 'select * from chart';
	//传参数
	var sqlArr = [];
	//回调
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'list': data
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//获取指定分类的文章
getPostCate = (req, res) => {
	let {
		id
	} = req.query;
	//查表cate_id=xx 的数据
	var sql = 'select * from post where cate_id=?';
	var sqlArr = [id];
	var callBack = (err, data) => {
		if (err) {
			console.log('错了')
		} else {
			res.send({
				'list': data
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}





module.exports = {
	getCate,
	getPostCate,
	getLoginUser,
	getLoginRoutes
}